package sioclient;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Requester类是请求程序，该类的run方法在连上socket后发生一个自身的标识符requestid， 
 * 在main函数中启动10个requester线程，来模拟多用户并发访问的情形。
 * 
 * @author Xinz
 *
 */
public class SIOClient extends Thread {

	private String address = "localhost";
	
	private int port = 8888;
	
	private int requestId;
	
	public SIOClient(int requestId) {
		this.requestId = requestId;
	}
	
	@Override
	public void run() {
		try {
			Socket socket = new Socket(address, port);
			socket.getOutputStream().write(requestId);
			sleep(5000);
			socket.close();
		}
		catch(UnknownHostException e) {
			e.printStackTrace();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
		catch(InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("No." + requestId + "was sent...");
	}
	
	public static void main(String args[]) {
		for(int i = 0; i < 10; ++i) {
			new SIOClient(i).start();
		}
	}
}
